package com.example.healkep.fragment;

public class ConnectNulls {

    public static Double[] Transform_nums(Double[] nums) {
        int length = nums.length;
        int start = 1;
        int last = 1;

        //找到第一个不为空的点（针对体重，体重下标和孕周是对应的），下标1——42，length=43
        for (int i = 1; i < length; i++) {
            if (nums[i] != null) {
                start = i;
                break;
            }
        }
        //找到最后一个不为空的点
        for (int i = length-1; i > 0; i--) {
            if (nums[i] != null) {
                last = i;
                break;
            }
        }

        int end = start;
        //start，end是两个中间均为空的非空点下标
        do {
            for (int i = start+1; i <= last; i++) {   //找第一个空点前的非空点
                if (nums[i] == null) {
                    start = i-1;
                    //遇到第一个空点，找到end的下标
                    end = start;
                    for (int j = i + 1; j <= last; j++) {
                        if (nums[j] != null){
                            end = j;
                            break;
                        }
                    }
                    break;
                }
            }
            int lenthOfNulls = end - start;
            //如果第一个非空点后全为非空或者只有一个非空点/全是空点
            if(lenthOfNulls == 0 || (last-start) == 0) break;
            double add = (nums[end] - nums[start]) / lenthOfNulls;
            //填补中间的空值
            for (int i = start + 1; i < end; i++) {
                nums[i] = nums[start] + (i - start) * add;
            }
            start = end;
        } while (end != last);

        return nums;
    }
}
